<?php
/*This is ajax file for saving result in draft*/
require_once "includes/config.php";

$page                       = $_REQUEST['page'];


if($page=='sell_product')
{
    $pro_subcategory            = $_REQUEST['pro_subcategory'];
    $pro_sub_subcategory        = $_REQUEST['pro_sub_subcategory'];
    if(empty($pro_sub_subcategory))
        $category               = $pro_subcategory;
    else
        $category               = $pro_sub_subcategory;
    
    
    $pro_name                   = $_REQUEST['pro_name'];
    $pro_description            = $_REQUEST['pro_description'];
    $pro_price                  = $_REQUEST['pro_price'];
    $pro_price	                = str_replace("$","",$pro_price);
   
    $bargain_price_option       = $_REQUEST['bargain_price_option'];
    $pro_courior                = $_REQUEST['pro_courior'];
    $pro_ship                   = $_REQUEST['pro_ship'];
    if($pro_ship==0 || $pro_ship=='')
        $shipping = 'Free';
    else
    {
        $shipping = $pro_ship;
        $shipping	=	str_replace("$","",$shipping);
    }
    
    $ship_status                = $_REQUEST['ship_status'];
    $localPickup                = $_REQUEST['localPickup'];
    $pro_commission             = $_REQUEST['pro_commission'];
    $pro_commission		= str_replace("$","",$pro_commission);
    
    $bargain_commission_option  = $_REQUEST['bargain_commission_option'];
    $pro_quantity               = $_REQUEST['pro_quantity'];
    $pro_condition              = $_REQUEST['pro_condition'];
    $sales_tax                  = $_REQUEST['sales_tax'];
    $pro_policy                 = $_REQUEST['pro_policy'];
    $remarks                    = $_REQUEST['remarks'];
    $amazon_name                = $_REQUEST['amazon_name'];
    $gift_name                  = $_REQUEST['gift_name'];
    $gift_description           = $_REQUEST['gift_description'];
    $userId                     = $_REQUEST['user_id'];
    

    $draft_name = 'Draft_'.date('Y-m-d').date('h:i:s');
    $sql_addpro="INSERT INTO tbl_drafts set
											draft_name='$draft_name',
											product_name='$pro_name',
											category_id='$category',
											product_price='$pro_price',
											commission='$pro_commission',
											bargain_price_option='$bargain_price_option',
											localPickup='$localPickup',
											bargain_commission_option='$bargain_commission_option',
											amazon_name='$amazon_name',
											gift_name='$gift_name',
											gift_description='$gift_description',
											bargain_amount='$pro_bargain',
											product_description='$pro_description',
											remarks='$remarks',
											quantity='$pro_quantity',
											shipping='$shipping',
											sales_tax='$sales_tax',
											pro_condition='$pro_condition',
											posted_by='$userId',
											return_id='$pro_policy',
											courior_id='$pro_courior'";
     $rst_addpro=mysql_query($sql_addpro);
    
	 $subject="Reminder about saved draft";
	 $sqlEmail="select * from tbl_emails where email_type='Save Draft'";
	 $resEmail=mysql_query($sqlEmail);
	 $rowEmail=mysql_fetch_array($resEmail);
	 
	 $selUser=mysql_fetch_array(mysql_query("select * from tbl_users where user_id='".$userId."'"));
	 $fullname=$selUser['first_name']." ".$selUser['last_name'];
	 
	 $message=str_replace('%fname%',$selUser['first_name'],str_replace('%lname%',$selUser['last_name'],$rowEmail['email_body']));
	 $headers = "From: Swapen.com<mail@swapen.com> \r\n";
	 $headers .= 'MIME-Version: 1.0' . "\r\n";
	 $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
	 mail($selUser['email'],$subject, $message, $headers);
    
    if($rst_addpro)
    {
        $sql = "select product_name,draft_id from tbl_drafts where posted_by='$userId'";
        $draft = mysql_query($sql) or die(mysql_error());
        $cnt_draft = mysql_num_rows($draft);
        if($cnt_draft > 0) {
            $option='<select style="width:150px;" id="saved_draft" onchange="applyDraft(this.value)"><option value="">Saved Drafts</option>';
            while($row = mysql_fetch_array($draft))
            {
                $option.='<option value="'.$row['draft_id'].'">'.$row['product_name'].'</option>';
            }
            $option.='</select>';
        }
        echo $option."###succ";
    }
    else
        echo '###err';
}
?>